package com.guldan.jaina.dto.progress;

import com.guldan.core.constants.CommonConstant;
import com.guldan.core.redis.CacheService;
import com.guldan.core.spring.ContextHolder;
import com.guldan.core.utils.MathUtil;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Created by Silas.
 * Date: 2016/12/31
 * Time: 12:31
 */
public class BarDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer typeId;

    private String barId;

    private Integer total;

    private Integer finish = 0;

    private Integer success = 0;

    private Integer fail = 0;

    private Boolean isOver = false;

    private float percent = 0;

    private String msg = "";

    public void finishAdd(Boolean isSuccess) {
        synchronized (this) {  //保证线程同步访问变量
            this.finish++;
            if (isSuccess)
                success++;
            else
                fail++;
            CacheService cacheService = (CacheService) ContextHolder.getBean("cacheService");
            //log.info("bardtofinishadd:" + this.toString());
            cacheService.putCache(this.barId, this, CommonConstant.TIME_SECOND_ONEDAY);   //将bardto缓存进redis，缓存1天
            //log.error(this.toString());
        }
    }

    public BarDTO() {
    }

    public BarDTO(String barId, Integer typeId, Integer total) {
        this.total = total;
        this.barId = barId;
        this.typeId = typeId;
        //log.info("initbardto success" + this.toString());
    }

    public float getPercent() {
        percent = MathUtil.divide(new BigDecimal(this.finish), this.total, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).floatValue();
        if (percent > 100) percent = 100;
        return percent;
    }

    public String getBarId() {
        return barId;
    }

    public void setBarId(String barId) {
        this.barId = barId;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getFinish() {
        return finish;
    }

    public void setFinish(Integer finish) {
        this.finish = finish;
    }

    public Integer getSuccess() {
        return success;
    }

    public void setSuccess(Integer success) {
        this.success = success;
    }

    public Integer getFail() {
        return fail;
    }

    public void setFail(Integer fail) {
        this.fail = fail;
    }

    public void setPercent(float percent) {
        this.percent = percent;
    }

    public Boolean getOver() {
        if (this.finish >= this.total)
            isOver = true;
        else
            isOver = false;
        return isOver;
    }

    public void setOver(Boolean over) {
        isOver = over;
    }

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("barId=" + this.barId);
        sb.append(" total=" + this.total);
        sb.append(" finish=" + this.finish);
        sb.append(" success=" + this.success);
        sb.append(" fail=" + this.fail);
        return sb.toString();
    }

}
